/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.teradata.olingo.provider;

import com.teradata.olingo.entities.ApiMetadata;
import com.teradata.olingo.service.EdmTypeTrnsfService;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.*;
import org.apache.olingo.commons.api.ex.ODataException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GenericEntityTypeProvider {

  @Autowired
  EdmTypeTrnsfService edmTypeTrnsfService;

  public CsdlEntityType getEntityType(FullQualifiedName entityTypeName) throws ODataException {
    List<ApiMetadata> apiMetadataList = edmTypeTrnsfService.getMetadataFromDB().get(entityTypeName.getName());
    if (apiMetadataList != null) {
      //      //create EntityType properties
      List<CsdlProperty> csdlPropertys = new ArrayList<CsdlProperty>();
      List<CsdlPropertyRef> csdlPropertyRefs = new ArrayList<CsdlPropertyRef>();

      for (ApiMetadata apiMetadata : apiMetadataList) {

        CsdlProperty csdlProperty = new CsdlProperty();
        csdlProperty.setName(apiMetadata.getFieldAlias())
                .setType(EdmPrimitiveTypeKind.valueOfFQN("Edm." + apiMetadata.getEdmType()).getFullQualifiedName());

        if (apiMetadata.getMaxLen() != null) {
          csdlProperty.setMaxLength(apiMetadata.getMaxLen());
        }

        if (apiMetadata.getPrec() != null) {
          csdlProperty.setPrecision(apiMetadata.getPrec());
        }

        if (apiMetadata.getScale() != null) {
          csdlProperty.setScale(apiMetadata.getScale());
        }

        if (apiMetadata.getIsntUnicode() != null) {
          csdlProperty.setUnicode(apiMetadata.getIsntUnicode());
        }

        if (apiMetadata.getNullable() != null) {
          csdlProperty.setNullable(apiMetadata.getNullable());
        }

        if (apiMetadata.getIsntCollection() != null) {
          csdlProperty.setCollection(apiMetadata.getIsntCollection());
        }

        if (apiMetadata.getDefaultValue() != null) {
          csdlProperty.setDefaultValue(apiMetadata.getDefaultValue());
        }

        if (apiMetadata.getMimeType() != null) {
          csdlProperty.setMimeType(apiMetadata.getMimeType());
        }

        csdlPropertys.add(csdlProperty);

        if (apiMetadata.getSyncMustType()) {
          CsdlPropertyRef propertyRef = new CsdlPropertyRef();
          propertyRef.setName(apiMetadata.getFieldAlias());
          csdlPropertyRefs.add(propertyRef);
        }
      }

      // configure EntityType
      CsdlEntityType entityType = new CsdlEntityType();
      entityType.setName(entityTypeName.getName());
      entityType.setProperties(csdlPropertys);
      if (!csdlPropertyRefs.isEmpty()) {
        entityType.setKey(csdlPropertyRefs);
      }

      return entityType;
    }

    return null;
  }
}
